package slr.comment.portlet.util;

import java.util.ArrayList;
import java.util.List;

import slr.comment.model.SlrAnchor;
import slr.comment.model.SlrComment;
import slr.comment.model.SlrDocument;
import slr.comment.model.SlrDocumentStructure;
import slr.comment.portlet.ui.SLRCommentForm;
import slr.comment.portlet.ui.SLRCommentsTable;
import slr.comment.portlet.util.Lang;
import slr.comment.service.SlrCommentLocalServiceUtil;
import slr.comment.service.SlrDocumentLocalServiceUtil;
import slr.comment.service.SlrDocumentStructureLocalServiceUtil;

import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.Validator;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

/**
 * This is Utility class for SLR-Comments.
 * 
 * @author TransIT mPower Labs
 * 
 */
public class SLRCommentsUtil {
	private static final long serialVersionUID = 409749180139391688L;

	/**
	 * This method displays the new window with SLR Comment Form.
	 * 
	 * @param slrAnchor
	 * @param slrDocId
	 * @param second
	 * @param type
	 */
	public void addCommentsDialog(final SlrAnchor slrAnchor, final long slrDocId, VerticalLayout second, String type) {
		final Window subwindow = new Window();
		final SLRCommentForm commentForm = new SLRCommentForm(slrAnchor, slrDocId, second, subwindow, null, type);
		subwindow.center();
		subwindow.setModal(true);
		subwindow.setWidth(60, Window.UNITS_PERCENTAGE);
		subwindow.addComponent(commentForm);
		second.getApplication().getMainWindow().addWindow(subwindow);
	}

	/**
	 * List of Buttons are added to the SLRExplorerContainer.
	 * 
	 * @param slrCommentsTable
	 * @param slrAnchor
	 * @param slrDocId
	 * @param second
	 * @param type
	 * @return HorizontalLayout with buttons added to the layout.
	 */
	@SuppressWarnings("serial")
	public HorizontalLayout getButtons(final SLRCommentsTable slrCommentsTable, final SlrAnchor slrAnchor,
			final long slrDocId, final VerticalLayout second, final String type) {
		HorizontalLayout buttons = new HorizontalLayout();
		buttons.setSpacing(true);

		buttons.setHeight(25, Button.UNITS_PERCENTAGE);
		final Button addComment = new Button(Lang.getKey("slr.comment.add-comments"));
		final Button save_reordering = new Button(Lang.getKey("slr.comment.save-reordering-button"));
		final Button quit_reordering = new Button(Lang.getKey("slr.comment.quit-reordering-button"));
		final Button reordering = new Button(Lang.getKey("slr.comment.reorder-button"));
		save_reordering.setEnabled(false);
		quit_reordering.setEnabled(false);

		addComment.addListener(new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				addCommentsDialog(slrAnchor, slrDocId, second, type);
			}
		});

		save_reordering.addListener(new Button.ClickListener() {
			@SuppressWarnings("unchecked")
			public void buttonClick(ClickEvent event) {
				List<SlrComment> commentList = null;
				try {
					commentList = (List<SlrComment>) slrCommentsTable.anchorTable.getItemIds();
					SlrComment slrComment = null;
					List<SlrComment> tmp = new ArrayList<SlrComment>();
					for (int i = 0; i < commentList.size(); i++) {
						slrComment = (SlrComment) commentList.get(i);
						slrComment.setSlrCommentOrder(i + 1);
						tmp.add(i, slrComment);
						SlrCommentLocalServiceUtil.deleteSlrComment(slrComment);
					}
					int tmpSize = tmp.size();
					for (int i = 0; i < tmpSize; i++) {
						slrComment = SlrCommentLocalServiceUtil.addSlrComment(tmp.get(i));
					}

					slrCommentsTable.anchorTable.getApplication().getMainWindow()
							.showNotification(Lang.getKey("slr.comment.successfully-reordered-comment-list"));
					SLRCommentForm.replaceOldComponenet(slrAnchor, second, type);
					addComment.setEnabled(true);
					quit_reordering.setEnabled(false);
					save_reordering.setEnabled(false);
				} catch (Exception e) {
					_log.error(e.getMessage());
				}

			}
		});

		quit_reordering.addListener(new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				try {
					slrCommentsTable.setRowDraggable(Lang.getKey("slr.comment.quit-reordering"));
					save_reordering.setEnabled(false);
					addComment.setEnabled(true);
					quit_reordering.setEnabled(false);
					SLRCommentForm.replaceOldComponenet(slrAnchor, second, type);
				} catch (Exception e) {
					_log.error(e.getMessage());
				}
			}
		});

		reordering.addListener(new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {

				try {
					slrCommentsTable.setRowDraggable(Lang.getKey("slr.comment.reorder"));
					save_reordering.setEnabled(true);
					quit_reordering.setEnabled(true);
					addComment.setEnabled(false);
				} catch (Exception e) {
					_log.error(e.getMessage());
				}

			}
		});

		buttons.addComponent(addComment);
		buttons.addComponent(reordering);
		buttons.addComponent(save_reordering);
		buttons.addComponent(quit_reordering);
		buttons.setMargin(false, false, false, true);
		return buttons;
	}

	/**
	 * To get the SLRDocumentId corresponding to SLR Anchor
	 * 
	 * @param slrAnchor
	 * @return long
	 */
	public static long getSlrDocId(SlrAnchor slrAnchor) {
		List<SlrDocumentStructure> slrDocumentStructureList = null;
		SlrDocumentStructure slrDocStr = null;
		long slrDocTypId;
		List<SlrDocument> slrDocumentList = null;
		SlrDocument slrDocument = null;
		long slrDocId = 0l;
		try {
			slrDocumentStructureList = (List<SlrDocumentStructure>) SlrDocumentStructureLocalServiceUtil
					.findByAnchorId(slrAnchor.getSlrAnchorId());
			if (Validator.isNotNull(slrDocumentStructureList) && slrDocumentStructureList.size() != 0) {
				slrDocStr = (SlrDocumentStructure) slrDocumentStructureList.get(0);
				slrDocTypId = slrDocStr.getSlrDocumentTypId();
				slrDocumentList = SlrDocumentLocalServiceUtil.findByDocumentTypId(slrDocTypId);
				if (Validator.isNotNull(slrDocumentList) && slrDocumentList.size() != 0) {
					slrDocument = (SlrDocument) slrDocumentList.get(0);
					slrDocId = slrDocument.getSlrDocumentId();
				}
			}
		} catch (Exception e) {
			_log.error(e.getMessage());
		}
		return slrDocId;
	}
	
	 private static Log _log = LogFactoryUtil.getLog(SLRCommentsUtil.class);
}